import axios from 'axios'
import store from "../store";
import {getToken, removeToken} from "./auth";
import { Message, MessageBox, Notification } from 'element-ui'
import baseURL from "./baseURLConfig";

const request = axios.create({
    baseURL: baseURL,
    timeout: 10000
})

axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

// 请求拦截器
request.interceptors.request.use( config => {
        if (store.getters.token){
            //请求需携带token
            config.headers['Authorization'] = getToken();
        }
    return config;
}, function (error) {
    return Promise.reject(error);
})


// 响应拦截器
request.interceptors.response.use( response  => {
        const res = response.data;
        if (res.code !== 200){
            Notification({
                title: '操作失败',
                message: res.msg,
                type: 'error',
                duration: 3 * 1000
            })
            return Promise.reject('error')
        }else {
            return response.data;
        }
},  error => {
    console.log(error.response.data)
    const code = error.response.data.code;
    if (code === 401){
        Notification({
            title: '会话过期',
            message: '请先重新登录',
            type: 'error',
            duration: 3 * 1000,
        })
        removeToken();
        this.$router.push( {path: '/login'} )
    }
    if (error.response.data.status === 404){
        Notification({
            message: '找不到资源',
            type: 'error',
            duration: 3 * 1000,
        })
    }
    return Promise.reject(error);
})

// GET
export function get(url, params) {
    return new Promise(
        (resolve,reject) => {
            request.get(url, {params: params})
                .then(res => {
                    // console.log('get请求封装res',res)
                    resolve(res.data)
                }).catch(err => {
                    reject(err.data)
            })
        }
    )
}

// POST
export function post(url, data) {
    return new Promise(
        (resolve,reject) => {
            request.post(url, data)
                .then(res => {
                    // console.log('post请求封装res',res)
                    resolve(res.data)
                }).catch(err => {
                reject(err.data)
            })
        }
    )
}

// DELETE
export function del(url, params) {
    return new Promise(
        (resolve,reject) => {
            request.delete(url, params)
                .then(res => {
                    // console.log('delete请求封装res',res)
                    resolve(res.data)
                }).catch(err => {
                reject(err.data)
            })
        }
    )
}

// PUT
export function put(url, data) {
    return new Promise(
        (resolve,reject) => {
            request.put(url, data)
                .then(res => {
                    // console.log('put请求封装res',res)
                    resolve(res.data)
                }).catch(err => {
                reject(err.data)
            })
        }
    )
}

export default request;
